'''
测试接口:系统操作日志/搜索
作者:王鑫
'''
import json
from unittest import TestCase
from jsonpath import jsonpath

from parameterized import parameterized

from utils.reader import Reader
from utils.session import Session


class SysOperateLogFindPageTest(TestCase):
    '''
    系统操作日志搜索接口测试类
    作者:王鑫
    '''
    session = None
    url = "http://120.53.243.26:8009/api/sysOperateLog/findPage"
    header = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.session = Session.get_session()
        cls.header = Session.get_header()

    @classmethod
    def tearDownClass(cls) -> None:
        cls.session.close()

    @parameterized.expand(Reader.read_excel('wx.xlsx',min_row=2,max_row=8,min_col=9,max_col=12))
    def test_sys_operate_log_find_page1(self, data, expcted_status_code, verify_point, expected_value):
        data = json.loads(data)

        # 发送请求
        resp = self.session.post(url=self.url, headers=self.header, json=data)

        body = resp.json()

        # 根据验证点提取出实际数据
        real_value = jsonpath(body, f"$..{verify_point}")
        real_value = real_value[0]

        # 断言检验响应状态码
        self.assertEqual(expcted_status_code, resp.status_code)
        # 断言检验响应正文的验证点
        if expected_value == '非空列表':
            self.assertNotEqual(0,len(real_value))
        else:
            self.assertEqual(0,len(real_value))


    @parameterized.expand(Reader.read_excel('wx.xlsx',min_row=9,max_row=15,min_col=9,max_col=12))
    def test_sys_operate_log_find_page2(self, data, expcted_status_code, verify_point, expected_value):
        try:
            data = json.loads(data)
        except Exception:
            data = data.replace('}}}', '0}}}')
            data = json.loads(data)
            data['searchDto']['searchExample']['userNo'] = None

        # 发送请求
        resp = self.session.post(url=self.url, headers=self.header, json=data)
        body = resp.json()

        # 根据验证点提取出实际数据
        real_value = jsonpath(body, f"$..{verify_point}")
        real_value = real_value[0]

        # 断言检验响应状态码
        self.assertEqual(expcted_status_code, resp.status_code)
        # 断言检验响应正文的验证点
        if expected_value == '不为0':
            self.assertNotEqual(0, real_value)
        else:
            self.assertEqual(expected_value, real_value)

